package com.gxa.saas.controller;

import com.gxa.saas.pojo.ClientGetPrice;
import com.gxa.saas.utils.JsonResult;
import com.gxa.saas.service.ClientGetPriceService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * @Author: yx
 * @Date: 2022/6/10 10:52
 */
@RestController
@RequestMapping(value = "/get")
@CrossOrigin
public class ClientGetPriceController {

    @Autowired
    ClientGetPriceService clientGetPriceService;

    @ApiOperation("查询全部")
    @PostMapping("/selectAll")
    public Map selectAll() {
        List<ClientGetPrice> clientGetPrices = clientGetPriceService.selectAll();
        Integer totalCount = clientGetPriceService.selectAllNumber();
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.set("totalCount", totalCount);
        jsonResult.setData(clientGetPrices);
        return jsonResult.getMap();
    }

    @ApiOperation("客户匹配价格体系的分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "offset", value = "查询页数", paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "size", value = "每页查询数量", paramType = "query", dataType = "int")
    })
    @PostMapping("/selectByPage")
    public Map selectByPage(@RequestParam("offset") Integer offset, @RequestParam("size") Integer size) {
        List<ClientGetPrice> clientGetPrices = clientGetPriceService.selecrByPage(offset, size);
        Integer totalCount = clientGetPriceService.selectAllNumber();
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.set("totalCount", totalCount);
        jsonResult.setData(clientGetPrices);
        return jsonResult.getMap();
    }

    /*@ApiOperation("添加客户匹配价格体系")
    @ApiImplicitParam(name = "clientGetPrice", value = "添加的对象", paramType = "body", dataType = "ClientGetPrice")
    @PostMapping("/insert")
    public Map insert(@RequestBody ClientGetPrice clientGetPrice) {
        clientGetPriceService.insert(clientGetPrice);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("添加成功");
        return jsonResult.getMap();
    }*/

    @ApiOperation("修改客户匹配价格体系")
    @ApiImplicitParam(name = "clientGetPrices", value = "修改好的的对象",paramType = "body",dataType = "ClientGetPrice")
    @PostMapping("/update")
    public Map update(@RequestBody ClientGetPrice[] clientGetPrices) {

        if (!StringUtils.isEmpty(clientGetPrices)) {
            for (int i = 0; i < clientGetPrices.length; i++) {
                System.out.println(clientGetPrices[i]);
                clientGetPriceService.update(clientGetPrices[i]);
            }
        }
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("修改成功");
        return jsonResult.getMap();
    }

}
